var router = require("koa-router")();
var utils = require("./../utils/utils");
var fetch = require("node-fetch");

// 模块导入
var superagent = require("superagent");
var cheerio = require("cheerio");

const baiduUrl = "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su";
router.prefix("/suggestion");
// 登录请求
router.get("/keyword", async (ctx, next) => {
  const { keyword } = ctx.query;
  const data = await GetSug(baiduUrl, keyword, (res) => console.log(res));
  console.log(data);
  ctx.body = utils.success(data);
});
function cb(obj) {
  console.log(obj);
}

function GetSug(url, keyword) {
  return new Promise((resolve, reject) => {
    superagent
      .get(url)
      .accept("application/json")
      .query({ wd: keyword })
      .end((e, res) => {
        try {
          var $ = cheerio.load(res.text);
          console.log("data=>", res.text);
          resolve(data);
        } catch (error) {
          console.log(error);
          resolve({ success: error });
        }
      });
  });
}

module.exports = router;
